var canvas = document.getElementById("mycanvas");
var h3 = document.getElementById("h3");
var p = document.getElementById("p1");
var game = document.getElementById("game");
var guanbi = document.getElementById("guan");
var audio = document.getElementById("audio");
var fangxia = document.getElementById("fangxia");
var shengli = document.getElementById("shengli");
//audio.volume = 1;
//audio.loop = "loop";
//audio.play();

canvas.width = 500;
canvas.height = 500;
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#ff8c00";
ctx.fillRect(0, 0, canvas.width, canvas.height);
ctx.strokeStyle = "#000000";
ctx.strokeRect(10, 10, 480, 480);
var wzq = [];
var isbai = true;
drawqipan();
init();

function newgame() {
	ctx.clearRect(0, 0, 500, 500)
	ctx.fillStyle = "#ff8c00";
	ctx.fillRect(0, 0, canvas.width, canvas.height);
	ctx.strokeStyle = "#000000";
	ctx.strokeRect(10, 10, 480, 480);
	game.style.display = "none";
	drawqipan();
	init();
}
canvas.onclick = function() {
	var x = event.offsetX - 25;
	var y = event.offsetY - 25;
	fangxia.play();
	for(var m = 0; m < wzq.length; m++) {
		for(var n = 0; n < wzq[m].length; n++) {
			var c = Math.sqrt(Math.pow(m * 32 - x, 2) + Math.pow(n * 32 - y, 2));
			activeQizi(m, n);
			if(c <= 15 && wzq[m][n] == 0) {
				wzq[m][n] = isbai ? 1 : 2;
				ctx.translate(25, 25);
				ctx.beginPath();
				ctx.arc(m * 32, n * 32, 15, 0, 2 * Math.PI);
				//							ctx.moveTo(m * 32 - 16, n * 32 - 16)
				//							ctx.fillRect(m * 32 - 16, n * 32 - 16, 32, 32)
				//							ctx.stroke()
				ctx.fillStyle = isbai ? "#ffffff" : "#000000";
				ctx.fill();
				ctx.translate(-25, -25);

				isbai = !isbai;
				isbai ? h3.innerHTML = "请白方下棋" : h3.innerHTML = "请黑方下棋";
				if(yanzheng(n, m)) {
					h3.innerHTML = "白先黑后"
					game.style.display = "block";
					shengli.play();
					isbai = true ? p.innerHTML = "白方胜利！" : p.innerHTML = "黑方胜利！"
				}

				break;
			}
		}
	}

}
//设置刚下的棋子效果
function activeQizi(a, b) {
	if(wzq[a][b] == 1) {
		ctx.shadowBlur = 10;
		ctx.shadowColor = "#000000";
	} else if(wzq[a][b] == 2) {
		ctx.shadowBlur = 10;
		ctx.shadowColor = "#FFFFFF";
	}

}

function init() {
	wzq = [];
	for(var i = 0; i < 15; i++) {
		wzq[i] = [];
		for(var j = 0; j < 15; j++) {
			wzq[i][j] = 0;
		}
	}
}

function drawWZQ() {
	ctx.translate(25, 25);
	for(var i = 0; i < wzq.length; i++) {
		for(var j = 0; j < wzq.length; j++) {
			if(wzq[i][j] == 1) {
				//画白棋

				ctx.beginPath();
				ctx.arc(i * 32, j * 32, 15, 0, 2 * Math.PI);
				ctx.fillStyle = "white";
				ctx.fill();
			} else if(wzq[i][j] == 2) {
				//画黑棋

				ctx.beginPath();
				ctx.arc(i * 32, j * 32, 15, 0, 2 * Math.PI);
				ctx.fillStyle = "black";
				ctx.fill();
			}

		}
	}

	ctx.translate(25, 25);
}

function drawqipan() {
	for(var i = 0; i < 15; i++) {
		ctx.beginPath();
		ctx.moveTo(25, i * 32 + 25);
		ctx.lineTo(474, i * 32 + 25)
		ctx.stroke();
	}
	for(var j = 0; j < 15; j++) {
		ctx.beginPath();
		ctx.moveTo(j * 32 + 25, 25);
		ctx.lineTo(j * 32 + 25, 474)
		ctx.stroke();
	}
}

function yanzheng(x, y) {
	//横向
	var bainum = 0;
	var heinum = 0;
	for(var i = 0; i < 15; i++) {
		bainum = 0;
		heinum = 0;
		for(var j = 0; j < 15; j++) {
			if(wzq[i][j] == 1) {
				bainum++;
				heinum = 0;
				if(bainum >= 5) {
					console.log("白方胜");
					return true;
				}
			} else if(wzq[i][j] == 2) {
				bainum = 0;
				heinum++;
				if(heinum >= 5) {
					console.log("黑方胜");
					return true;
				}
			} else if(wzq[i][j] == 0) {
				bainum = 0;
				heinum = 0;
			}
		}
	}
	//纵向
	for(var i = 0; i < 15; i++) {
		bainum = 0;
		heinum = 0;
		for(var j = 0; j < 15; j++) {
			if(wzq[j][i] == 1) {
				bainum++;
				heinum = 0;
				if(bainum >= 5) {
					console.log("白方胜");
					return true;
				}
			} else if(wzq[j][i] == 2) {
				bainum = 0;
				heinum++;
				if(heinum >= 5) {
					console.log("黑方胜");
					return true;
				}
			} else if(wzq[j][i] == 0) {
				bainum = 0;
				heinum = 0;
			}
		}
	}
	//斜上
	var vy = x + y > 14 ? 14 : x + y;
	var vx = (x + y) - vy;
	bainum = 0;
	heinum = 0;
	for(; vy > 0 && vx < 15; vy--, vx++) {
		if(wzq[vy][vx] == 1) {
			bainum++;
			heinum = 0;
			if(bainum >= 5) {
				console.log("白方胜");
				return true;
			}
		} else if(wzq[vy][vx] == 2) {
			bainum = 0;
			heinum++;
			if(heinum >= 5) {
				console.log("黑方胜");
				return true;
			}
		} else if(wzq[vy][vx] == 0) {
			bainum = 0;
			heinum = 0;
		}
	}
	//斜下
	var vy = y - x < 0 ? 0 : y - x;
	var vx = x - y < 0 ? 0 : x - y;
	bainum = 0;
	heinum = 0;
	for(; vy < 15 && vx < 15; vy++, vx++) {
		if(wzq[vy][vx] == 1) {
			bainum++;
			heinum = 0;
			if(bainum >= 5) {
				console.log("白方胜");
				return true;
			}
		} else if(wzq[vy][vx] == 2) {
			bainum = 0;
			heinum++;
			if(heinum >= 5) {
				console.log("黑方胜");
				return true;
			}
		} else if(wzq[vy][vx] == 0) {
			bainum = 0;
			heinum = 0;
		}
	}
	return false;
}